﻿// MetaFieldGroupTreeView.cpp: 实现文件
//

#include "stdafx.h"
#include "MetaFieldGroupTreeView.h"
#include "resource.h"


// CMetaFieldGroupTreeView

IMPLEMENT_DYNCREATE(CMetaFieldGroupTreeView, CTreeView)

CMetaFieldGroupTreeView::CMetaFieldGroupTreeView()
{

}

CMetaFieldGroupTreeView::~CMetaFieldGroupTreeView()
{
}

BEGIN_MESSAGE_MAP(CMetaFieldGroupTreeView, CTreeView)
	ON_WM_CREATE()
	ON_NOTIFY_REFLECT(NM_CLICK, &CMetaFieldGroupTreeView::OnNMClick)
END_MESSAGE_MAP()


// CMetaFieldGroupTreeView 诊断

#ifdef _DEBUG
void CMetaFieldGroupTreeView::AssertValid() const
{
	CTreeView::AssertValid();
}

#ifndef _WIN32_WCE
void CMetaFieldGroupTreeView::Dump(CDumpContext& dc) const
{
	CTreeView::Dump(dc);
}
#endif
#endif //_DEBUG


// CMetaFieldGroupTreeView 消息处理程序


int CMetaFieldGroupTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_META_SCHEMA_TREE_24))
	{
		m_il.Create(24, 24, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		GetTreeCtrl().SetImageList(&m_il, TVSIL_NORMAL);
	}


	GetTreeCtrl().InsertItem("数据项组", 0, 0);
	Populate();

	return 0;
}


BOOL CMetaFieldGroupTreeView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此添加专用代码和/或调用基类
	cs.style |= TVS_HASLINES | TVS_SHOWSELALWAYS | TVS_HASBUTTONS | TVS_NOHSCROLL | TVS_EX_FADEINOUTEXPANDOS;

	return CTreeView::PreCreateWindow(cs);
}

void CMetaFieldGroupTreeView::Populate()
{
	const char* szPath = "E:\\Project\\Apps\\zChecker\\Dist\\conf\\LandCover30MetaMap.xml";

	ck::data::Schema* pSchema = NULL;
	ck::data::SchemaReader reader;
	pSchema = reader.Read(szPath);

	Populate(pSchema);
}

void CMetaFieldGroupTreeView::Populate(ck::data::Schema* pSchema)
{
	ck::data::Field* pField = NULL;
	ck::data::EnumField* pFields = NULL;
	ck::data::Group* pGroup = NULL;
	ck::data::EnumGroup* pGroups = NULL;

	CTreeCtrl& tree = GetTreeCtrl();

	pGroups = pSchema->GetGroups();
	pGroups->Reset();
	while ((pGroup = pGroups->Next()))
	{
		const char* szName = pGroup->GetName();
		
		HTREEITEM hItem = tree.InsertItem(szName, 1, 1, tree.GetRootItem());
		//tree.SetItemData(hItem, (DWORD_PTR)pGroup);
		//pGroup->AddRef();

		pFields = pGroup->GetFields();
		pFields->Reset();
		while ((pField = pFields->Next()))
		{
			tree.InsertItem(pField->GetName(), 2, 2, hItem);
		}
		pFields->Release();
		
		tree.Expand(hItem, TVE_EXPAND);
	}
	pGroups->Release();

	tree.Expand(tree.GetRootItem(), TVE_EXPAND);
}

void CMetaFieldGroupTreeView::OnNMClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;
}
